package kislo.dima.task3;

import java.util.Arrays;
import java.util.Random;

/***
 * 
 * @author Dima Kislo
 *
 */

public class Search {

	public static void main(String[] args) {
		int m = Integer.parseInt(args[0]);
		int[] ar = new int[m];
		for (int i = 0; i < m; i++)
			ar[i] = new Random().nextInt(100);

		System.out.print("Default array:");
		for (int row : ar) {
			System.out.print(row + " ");
		}
		System.out.println();

		// sort array
		Arrays.sort(ar);
		// binary search
		binSearch(ar, m, Integer.parseInt(args[1]));
	}

	public static void binSearch(int[] ar, int m, int x) {
		int first = 0;
		int last = m;
		int midle;

		if ((m - 1) > Integer.MAX_VALUE)
			return;
		if (m == 0) {
			System.out.println("The array is empty");
			return;
		} else if (ar[0] > x) {
			System.out.println("The element is not found");
			return;
		} else if (ar[m - 1] < x) {
			System.out.println("The element is not found");
			return;
		}

		while (first < last) {
			midle = first + (last - first) / 2;
			if (x <= ar[midle])
				last = midle;
			else
				first = midle + 1;
		}

		if (ar[last] == x)
			System.out.println("The element is found!!!");
		else
			System.out.println("The element is not found");
	}
}
